package design_patterns.observer;

import java.util.ArrayList;
import java.util.List;

public class Subject {
	private List<Observer>observers = new ArrayList<Observer>();
	private int state;
	
	public int getState() {
		return state;
	}
	
	public void setState(int state) {
		this.state = state;
		//通知所有观察者
		notifyAllObserver();
	}
	public void attch(Observer observer){
		observers.add(observer);
	}
	
	public void notifyAllObserver(){
		for (Observer observer : observers) {
			//这儿是顺序执行的，需要避免：①避免循环调用②不要使用同步锁最好异步调用见file
			observer.update();
		}
	}
}
